package de.koller.worlders.gameplay.ability;

import de.koller.oceanFramework.glyph.GlyphScreenGL;
import de.koller.oceanFramework.tree.Child;
import de.koller.oceanFramework.tree.Mother;
import de.koller.worlders.WSea;
import de.koller.worlders.gameplay.GameplayPlayer;
import de.koller.worlders.gameplay.resource.FontValue;
import de.koller.worlders.render.IGLRender;
import org.lwjgl.opengl.GL11;

public class AbilityBarV extends WSea implements IGLRender {

	@Mother private GameplayPlayer gplayer;
	@Mother private AbilityBar abilityBar;
	
	@Child private final GlyphScreenGL gscreen = new GlyphScreenGL( 256, 256,
			new FontValue(), "de/koller/worlders/gameplay/resource/font.png" );

	@Override
	protected void init() {
		super.init();
		
		wroot.rendering.addGUIElement( this );
	}

	@Override
	protected void release() {
		super.release();
		
		wroot.rendering.removeGUIElement( this );
	}
	
	@Override
	public void renderGL( int elapsedTime ) {
		if( gplayer != null )
			if( wroot.inventory.isVisible() )
				return;
		
		int slot = 0;
		for( Ability a : gplayer.playerAbility )
			renderAbilityButton( a, slot++ );
	
		GL11.glColor3f( 1, 1, 1 );
		
		gscreen.updateBuffer();
		gscreen.getTexture().draw2D( 5, 5, 256, 256, 20 );
	}

	private void renderAbilityButton( Ability a, int slot ) {
		calcVisual( a );
		
		int bc = slot == abilityBar.getCurrentAbility()-1 ? 0xffffff00 : 0;
		int fc = slot == abilityBar.getCurrentAbility()-1 ? 0xff000000 : 0xffffffff;
		gscreen.getRenderer().setCurrentBackColor( bc );
		gscreen.getRenderer().setCurrentFontColor( fc );
		
		gscreen.getRenderer().renderGlyph( 0 + (4*slot), 0, a.getVisualX(), a.getVisualY() );
		gscreen.getRenderer().renderGlyph( 1 + (4*slot), 0, a.getVisualX()+1, a.getVisualY() );
		gscreen.getRenderer().renderGlyph( 2 + (4*slot), 0, a.getVisualX()+2, a.getVisualY() );

		gscreen.getRenderer().renderGlyph( 0 + (4*slot), 1, a.getVisualX(), a.getVisualY()+1 );
		gscreen.getRenderer().renderGlyph( 1 + (4*slot), 1, a.getVisualX()+1, a.getVisualY()+1 );
		gscreen.getRenderer().renderGlyph( 2 + (4*slot), 1, a.getVisualX()+2, a.getVisualY()+1 );

		gscreen.getRenderer().renderString( 0 + (4*slot), 2, "[" + (slot+1) + "]" );
	}
	
	private void calcVisual( Ability a ) {
		if( !(a.getVisualX() == -1 && a.getVisualY() == -1) )
			return;
		
		if( a instanceof NoAbility ) a.setVisual( 3, 9 );
		else if( a instanceof MineAbility ) a.setVisual( 6, 9 );
		else if( a instanceof EncAbility ) a.setVisual( 9, 9 );
		else a.setVisual( 0, 9 );
	}
	
}
